package com.jingdianjichi.project.application.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.project.application.convert.SubjectLabelDTOConverter;
import com.jingdianjichi.project.application.dto.SubjectLabelDTO;
import com.jingdianjichi.project.common.entity.Result;
import com.jingdianjichi.project.domain.bo.SubjectLabelBo;
import com.jingdianjichi.project.domain.service.SubjectLabelDomainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * ClassName:SubjectLabel
 * Package:com.jingdianjichi.subject.application.controller
 * Description:
 *
 * @Author fst
 * @Create 2024/9/26 20:28
 * @Version 1.0
 */

@RestController
@RequestMapping("subject/label/")
@Slf4j
public class SubjectLabelController {
    @Resource
    private SubjectLabelDomainService subjectLabelDomainService;

    @PostMapping("add")
    public Result add(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.add.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }

            //断言检查
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "分类id不可以为空");
            Preconditions.checkArgument(!(subjectLabelDTO.getLabelName().equals("")), "标签名不可以为空");
            Preconditions.checkNotNull(subjectLabelDTO.getSortNum(), "热点num不能为空");

            SubjectLabelBo subjectLabelBo = SubjectLabelDTOConverter.INSTANCE.convertDTOToBOLabel(subjectLabelDTO);

            subjectLabelDomainService.add(subjectLabelBo);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectLabelController.add.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("update")
    public Result update(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.update.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }

            //断言检查
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "标签id不可以为空");

            SubjectLabelBo subjectLabelBo = SubjectLabelDTOConverter.INSTANCE.convertDTOToBOLabel(subjectLabelDTO);

            subjectLabelDomainService.update(subjectLabelBo);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectLabelController.add.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }


    @PostMapping("delete")
    public Result delete(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.delete.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }

            //断言检查
            Preconditions.checkNotNull(subjectLabelDTO.getId(), "标签id不可以为空");


            SubjectLabelBo subjectLabelBo = SubjectLabelDTOConverter.INSTANCE.convertDTOToBOLabel(subjectLabelDTO);

            subjectLabelDomainService.delete(subjectLabelBo);

            return Result.ok(true);
        } catch (Exception e) {
            log.error("SubjectLabelController.delete.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

    @PostMapping("queryLabelByCategoryId")
    public Result<List<SubjectLabelDTO>> LabelByCategoryId(@RequestBody SubjectLabelDTO subjectLabelDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("SubjectCategoryController.queryLabelByCategoryId.dto:{}", JSON.toJSONString(subjectLabelDTO));
            }

            //断言检查
            Preconditions.checkNotNull(subjectLabelDTO.getCategoryId(), "标签id不可以为空");

            SubjectLabelBo subjectLabelBo = SubjectLabelDTOConverter.INSTANCE.convertDTOToBOLabel(subjectLabelDTO);

            List<SubjectLabelBo> subjectLabelBoList =subjectLabelDomainService.LabelByCategoryId(subjectLabelBo);

           List<SubjectLabelDTO> subjectLabelDTOList=SubjectLabelDTOConverter.INSTANCE.converBOTODTOLabelList(subjectLabelBoList);

            return Result.ok(subjectLabelDTOList);

        } catch (Exception e) {
            log.error("SubjectLabelController.LabelByCategoryId.error:{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }
    }

}
